package com.project.service.impl;

import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl extends BaseService implements IUserService {

    @Override
    public UserBean login(String account, String pwd) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean user = mapper.login(account,pwd);
        session.close();
        return user;
    }

    @Override
    public List<UserBean> findAllUser() {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> list = mapper.findAllUser();
        session.close();
        return list;
    }

    @Override
    public boolean addUser(UserBean user) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean userBean = mapper.checkAccount(user.getAccount());
        if (userBean == null){
            mapper.addUser(user);
            session.commit();
            session.close();
            return true;
        }
        return false;
    }

    @Override
    public void de(Integer id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.de(id);

        session.commit();
        session.close();
    }

    @Override
    public UserBean findById(Integer id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean user = mapper.findById(id);
        session.close();
        return user;
    }

    @Override
    public void changeLevel(Integer id, String level) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.changeLevel(id,level);

        session.commit();
        session.close();
    }

    @Override
    public void changePwd(Integer id, String newPwd) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.changePwd(id,newPwd);

        session.commit();
        session.close();
    }
}
